package com.laputacastle.dr.utils;

import org.springframework.util.DigestUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.Random;

/**
 * 公共工具类
 *
 * @author duanlongfei
 * @create 2019/7/25
 */
public final class Utils {
    //盐，用于混交md5
    private static final String slat = "&%5123***&&%%$$#@";

    private Utils() {
    }

    /**
     * 判断当前服务器是不是windows
     * 是返回true 不是返回 false
     *
     * @return boolean
     */
    public static boolean checkWindows() {
        // 判断系统是windows还是linux
        Properties prop = System.getProperties();
        String os = prop.getProperty("os.name");
        if (os != null && os.toLowerCase().indexOf("linux") > -1) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取6位随机数
     *
     * @return java.lang.String
     */
    public static String randomCode() {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    /**
     * 将对象转化为字节码
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static byte[] getBytesFromObject(Serializable obj) throws Exception {
        if (obj == null) {
            return null;
        }
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);
        return bo.toByteArray();
    }

    public static Object getObjectFromBytes(byte[] objBytes) throws Exception {
        if (objBytes == null || objBytes.length == 0) {
            return null;
        }
        ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);
        ObjectInputStream oi = new ObjectInputStream(bi);
        return oi.readObject();
    }

    /**
     * 数字转中文
     * @param src
     * @return
     */
    public static String int2chineseNum(int src) {
        final String num[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        final String unit[] = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String dst = "";
        int count = 0;
        while(src > 0) {
            dst = appString((appString(num[src % 10], unit[count])), dst);
            src = src / 10;
            count++;
        }
        return dst.replaceAll("零[千百十]", "零").replaceAll("零+万", "万")
                .replaceAll("零+亿", "亿").replaceAll("亿万", "亿零")
                .replaceAll("零+", "零").replaceAll("零$", "");
    }

    /**
     * 生成md5
     * @param str
     * @return
     */
    public static String getMD5(String str) {
        String base = str +"/"+slat;
        String md5 = DigestUtils.md5DigestAsHex(base.getBytes((Charset.forName("UTF-8"))));
        return md5;
    }

    /**
     * 拼接字符串
     * @param str1
     * @param str2
     * @return
     */
    public static String appString(String str1, String str2){
        StringBuilder sb = new StringBuilder();
        sb.append(str1);
        sb.append(str2);
        return sb.toString();
    }

}
